package com.train.quartz.executor;

import cn.hutool.json.JSONUtil;
import com.train.quartz.constants.ParamMapConstants;
import com.train.quartz.handler.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;

import java.util.Date;

@Slf4j
public class JobExecutor {

    private final Scheduler scheduler;

    public JobExecutor(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    //添加quartz任务
    public void addJob(String jobName, String groupName, Object jobParam, String cronExpress, String retryCount, String retryInterval) throws SchedulerException {
        if (scheduler == null) {
            return;
        }
        //创建定时任务
        JobDataMap jobDataMap = new JobDataMap();
        if (jobParam != null) {
            jobDataMap.put(ParamMapConstants.JOB_PARAM, JSONUtil.toJsonStr(jobParam));
        }
        jobDataMap.put(ParamMapConstants.JOB_NAME, jobName);
        jobDataMap.put(ParamMapConstants.RETRY_COUNT, retryCount);
        jobDataMap.put(ParamMapConstants.JOB_INTERVAL, retryInterval);
        JobDetail jobDetail = JobBuilder.newJob(JobHandler.class)
                .withIdentity(jobName, groupName)
                .setJobData(jobDataMap)
                .build();
        //创建触发器
        Trigger trigger = buildTrigger(jobName, groupName, cronExpress);
        //绑定到调度器
        scheduler.scheduleJob(jobDetail, trigger);
    }

    //删除quartz任务
    public void deleteJob(String jobName, String groupName) throws SchedulerException {
        if (scheduler == null) {
            return;
        }
        scheduler.pauseJob(new JobKey(jobName, groupName)); //暂停于job相关的所有触发器
        scheduler.deleteJob(new JobKey(jobName, groupName)); //删除jobdetail和所有相关触发器
//        scheduler.unscheduleJob(new TriggerKey(jobName)); //删除指定触发器
    }

    //暂停quartz任务
    public void pauseJob(String jobName, String groupName) throws SchedulerException {
        if (scheduler == null) {
            return;
        }
        scheduler.pauseJob(new JobKey(jobName, groupName)); //暂停于job相关的所有触发器
//        scheduler.pauseTrigger(new TriggerKey(jobName, groupName)); //暂停指定触发器
    }

    //恢复quartz任务
    public void resumeJob(String jobName, String groupName) throws SchedulerException {
        if (scheduler == null) {
            return;
        }
        scheduler.resumeJob(new JobKey(jobName, groupName)); //恢复job相关的所有触发器
//        scheduler.resumeTrigger(new TriggerKey(jobName)); //恢复指定触发器
    }

    //更新quartz任务
    public Date updateJob(String jobName, String groupName, String cronExpress) throws SchedulerException {
        if (scheduler == null) {
            return null;
        }
        Trigger trigger = buildTrigger(jobName, groupName, cronExpress);
        return scheduler.rescheduleJob(new TriggerKey(jobName, groupName), trigger);
    }

    //手动执行一次定时任务
    public void executeJob(String jobName, String groupName) throws SchedulerException {
        if (scheduler == null) {
            return;
        }
        scheduler.triggerJob(new JobKey(jobName, groupName));
    }

    //构建触发器
    public Trigger buildTrigger(String jobName, String groupName, String cronExpress) {
        return TriggerBuilder.newTrigger()
                .withIdentity(jobName, groupName)
                .usingJobData(ParamMapConstants.JOB_NAME, jobName)
                .usingJobData(ParamMapConstants.GROUP_NAME, groupName)
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpress))
                .build();
    }

    //检查定时任务是否存在
    public boolean exists(String jobName, String groupName) throws SchedulerException {
        return scheduler.checkExists(new JobKey(jobName, groupName));
    }
}
